/**
 * 考勤报表
 */

$(function() {
	$('.pxybtn').on('mouseover', function() {
		$(this).addClass('light');
	})
	$('.pxybtn').on('mouseleave', function() {
		$(this).removeClass('light');
	});
	
	$('.toolkit-item').on('change','.date input',function(){
		ton($('.date .start-time').val(),$('.date .end-time').val());
	})
	$('#this').on('click', function() {
		refreshPanel(getUrlPrefix('/work/attend/check?start='+getMonthStart(0)+"&end="+getMonthEnd()+"&ths=1"), "考勤报表")
	})
	$('#upper').on('click', function() {
		refreshPanel(getUrlPrefix('/work/attend/check?start='+getMonthStart(-1)+"&end="+getMonthEnd2(1)+"&ths=2"), "考勤报表")
	})
	//初始化计时，和部分样式
	as();
	
	$('.punch-clock').on('click',function(){
		var status=0;
		var type=$('.type').text();
		if(type=='签到') {
			var guitime=new Date(getMonthEnd()+" "+$('.go .gui-time').text());
			var shijitime=new Date(getMonthEnd()+" "+$('.punch-clock .t').text());
			if (shijitime>guitime) {
				status=1;
			}
		}else {
			var guitime=new Date(getMonthEnd()+" "+$('.out .gui-time').text());
			var shijitime=new Date(getMonthEnd()+" "+$('.punch-clock .t').text());
			if (shijitime<guitime) {
				status=2;
			}
		}
		
		//获取本地ip
		var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_=' + Math.random();
		$.getJSON(url, function (data) {
			var ip=data.Ip;//弹出本地ip
			$.post('/work/attend/save',{
				type:type,
				ip:ip,
				status:status,
				remark:'无申诉'
			},function(data){
				if (data!=null) {
					layer.msg("打卡成功！", {
						time : 1000
					});
					refresh(this);
				}
			})
	    });
	})
	
	
	$('.out').on('click','.chong',function(){
		var id=$(this).attr('data-id');
		var guitime=new Date(getMonthEnd()+" "+$('.out .gui-time').text());
		var shijitime=new Date();
		var status=0;
		if (shijitime<guitime) {
			var status=2;
		}
		$.post('/work/attend/save',{
			id:id,
			status:status,
			remark:'无申诉'
		},function(data){
			if (data!=null) {
				layer.msg("重新签退成功！", {
					time : 1000
				});
				refresh(this);
			}
		})
	})
	
	$('.xiao').on('click',function(){
		var t=$('.work-time').text();
		if ($(this).text()=='取消迟到') {
			$('.appeal .ap-h span').text(t+' 迟到')
		}else {
			$('.appeal .ap-h span').text(t+' 早退')
		}
		var aid=$(this).attr('data-id');
		laye(aid)
	})
	
	/*
	 * 申诉操作
	 */
	$('.attend').on('click','.btn-appeal',function(){
		var time=$(this).parent('td').siblings('td:eq(0)').text();
		var status=$(this).parent('td').siblings('td:eq(2)').text();
		var s=new Date(time);
		var t=getMonthEnd(time)+' 星期'+zu(s.getDay());
		$('.appeal .ap-h span').text(t+' '+status)
		var aid=$(this).attr('data-id');
		laye(aid)
	})
	
	//通过日历申诉
	$('.qian').on('click','.pull-right',function(){
		if ($(this).text()=='无操作'||$(this).text()=='已申诉') {
			return;
		}
		var time=$('.ts').text();
		var status=$('.qian .stat').text();
		var s=new Date(time);
		var t=getMonthEnd(time)+' 星期'+zu(s.getDay());
		$('.appeal .ap-h span').text(t+' '+status);
		var aid=$('.qian .caoz').attr('id');
		laye(aid);
	})
	$('.tui').on('click','.pull-right',function(){
		if ($(this).text()=='无操作'||$(this).text()=='已申诉') {
			return;
		}
		var time=$('.ts').text();
		var status=$('.tui .stat').text();
		var s=new Date(time);
		var t=getMonthEnd(time)+' 星期'+zu(s.getDay());
		$('.appeal .ap-h span').text(t+' '+status)
		var aid=$('.tui .caoz').attr('id');
		laye(aid)
	})
	/**
	 * 申诉处理
	 */
	$('.com').on('click','.btn-look',function(){
		var status=$(this).parent('td').siblings('td:eq(2)').text();
		var id=$(this).attr('data-id');
		$('.chuli .ap-h span').text(status);
		$('.chuli .ns').text($(this).parent('td').siblings('td:eq(0)').text());
		$('.chuli p').text($(this).attr('data-content'));
		laye2(id);
	})
	
	//查看退回申诉
	$('.seecom').on('click',function(){
		var time=$(this).parent('td').siblings('td:eq(0)').text();
		var status=$(this).parent('td').siblings('td:eq(2)').text();
		var s=new Date(time);
		var t=getMonthEnd(time)+' 星期'+zu(s.getDay());
		var name=$(this).attr('data-acname');
		if ($(this).attr('data-s')==2) {
			var id=$(this).attr('data-id');
			$.post('/work/attend/bywaid',{
				id:id
			},function(data){
				$('.appeal2 .jie span').text("申诉已退回");
				$('.appeal2 .ren span').text(name);
				$('.appeal2 .ti span').text(data.time);
				$('.appeal2 .shuo span').text(data.result);
				$('.appeal2 .we span').text(t+' '+status);
			})
			laye4(id);
		}
	})
	
	//日历更换日期
	/*$('#cc').calendar({
		onChange : function(date) {
			setTimeout(function() {
				cimg(2);
			}, 20);
		}
	});*/
	$('.calendarwe').on('click','.calendar-nav.calendar-prevmonth', function() {
		setTimeout(function() {
			cimg(2,null);
		}, 50);
	})
	// 下一个月
	$('.calendarwe').on('click','.calendar-nav.calendar-nextmonth', function() {
		setTimeout(function() {
			cimg(2,null);
		}, 50);
	})
	$('.calendarwe').on('click','.calendar-text', function() {
		$('.calendar-nav.calendar-menu-month').on('click', function() {
			setTimeout(function() {
				cimg(2,null);
			}, 50);
		});
	})
	$('.calendarwe').on('click','.calendar-other-month',function() {
		setTimeout(function() {
			cimg(2,null);
		}, 50);
	})
	/**
	 * 考勤日历
	 */
	$('.diawo').on('click',function(){
		cimg(1,null);
		//laye5();
	})
	
	/**
	 * 考勤报表模糊查寻
	 */
	$('.check').on('click','.search-btn',function(){
		refreshPanel('/work/attend/check?key='+$('.key').val()+'&did='+$('.dept option:selected').val(),
				"考勤报表")
	})
	//申诉管理模糊查寻
	$('.com').on('click','.search-btn',function(){
		var name=$('.name').val();
		refreshPanel('/work/attend/complaints?name='+name+'&st='+$('.st option:selected').val(),
		"申诉管理")
	})
	
	//判断是否是周日
	//pandfw()
	function pandfw(){
		var time=(new Date()).getDay();
		if (time==0||time==6){
			$('.punch-clock .type').remove();
			$('.punch-clock .t').remove();
			$('.punch-clock .xiuxi').css({
				display:'block'
			})
		}
	}
	$('.calendarwe').on('click','i',function(){
		if ($(this).attr('class')=="iconfont icon-xiukexuan") {
			layer.msg("今天休息！", {
				time : 1000
			});
			return;
		}
		if ($(this).attr('class')=='iconfont icon-jia1') {
			if ($(this).attr('remark')!="无申诉") {
				layer.msg($(this).attr('remark'), {
					time : 1000
				});
				return;
			}
			var time=$('.ts').text();
			var status=$(this).attr('type');
			var s=new Date(time);
			var t=getMonthEnd(time)+' 星期'+zu(s.getDay());
			$('.appeal .ap-h span').text(t+' '+status);
			var aid=$(this).attr('id3');
			laye(aid);
			return;
		}
		$('.model .ts').text(shieaw($(this).parent('td').attr('abbr')));
		if ($(this).attr('acw1')!=null) {
			var se=$('.model .qian .stat');
			se.css({
				color:'red'
			})
			var wew=$('.model .qian .caoz');
			wew.css({
				color:'#666',
			})
			wew.text('已申诉');
			if ($(this).attr('status1')=='3') {
				se.text('缺勤');
			}else {
				se.text('迟到');
			}
			
		}else if ($(this).attr('status1')==null) {
			var se=$('.model .qian .stat');
			se.css({
				color:'red'
			})
			se.text('缺勤');
			var wew=$('.model .qian .caoz');
			wew.css({
				color:'#29e',
				cursor: 'pointer'
			})
			wew.text('取消缺勤');
			wew.attr('id',0);
		}else if($(this).attr('status1')=='0'){
			var se=$('.model .qian .stat');
			se.css({
				color:'#333'
			})
			se.text('正常');
			var wew=$('.model .qian .caoz');
			wew.css({
				color:'#666',
			})
			wew.text('无操作');
		}else {
			var se=$('.model .qian .stat');
			se.css({
				color:'red'
			})
			se.text('迟到');
			var wew=$('.model .qian .caoz');
			wew.css({
				color:'#29e',
				cursor: 'pointer'
			})
			wew.text('取消迟到');
			wew.attr('id',$(this).attr('id1'));
		}
		
		if ($(this).attr('acw2')!=null) {
			var se=$('.model .tui .stat');
			se.css({
				color:'red'
			})
			var wew=$('.model .tui .caoz');
			wew.css({
				color:'#666',
			})
			wew.text('已申诉');
			if ($(this).attr('status2')=='4') {
				se.text('缺退');
			}else {
				se.text('早退');
			}
			
		}else if ($(this).attr('status2')==null) {
			var se=$('.model .tui .stat');
			se.css({
				color:'red'
			})
			se.text('缺退');
			var wew=$('.model .tui .caoz');
			wew.css({
				color:'#29e',
				cursor: 'pointer'
			})
			wew.text('取消缺退');
			wew.attr('id',0);
		}else if($(this).attr('status2')=='0'){
			var se=$('.model .tui .stat');
			se.css({
				color:'#333'
			})
			se.text('正常');
			var wew=$('.model .tui .caoz');
			wew.css({
				color:'#666',
			})
			wew.text('无操作');
		}else {
			var se=$('.model .tui .stat');
			se.css({
				color:'red'
			})
			se.text('早退');
			var wew=$('.model .tui .caoz');
			wew.css({
				color:'#29e',
				cursor: 'pointer'
			})
			wew.text('取消早退');
			wew.attr('id',$(this).attr('id2'));
		}
		laye6();
	})
	
	
});

//日记录
function laye6(){
	layer.open({
		type: 1,
		skin: 'demo-class',
		skin: 'yourclass',
		offset: '100px',
		title :'日记录',
		area: ['450px', '200px'], //宽高
		content: $(".attend .model"),
		closeBtn: 1,
		btn:['确定'],
		btnAlign: 'c', 
		yes: function(index2, layero){
			layer.close(index2);
		}
	});
	$('.layui-layer-ico').attr('href','javascript:void(0)');
}

//考勤日历
function laye5(){
	layer.open({
		type: 1,
		skin: 'demo-class',
		skin: 'yourclass',
		offset: '100px',
		title :'考勤日历',
		area: ['310px', '320px'], //宽高
		content: $(".attend .calendarwe"),
		closeBtn: 1,
		btn:['确定'],
		btnAlign: 'c', 
		yes: function(index2, layero){
			layer.close(index2);
		}
	});
	$('.layui-layer-ico').attr('href','javascript:void(0)');
}
//考勤日历添加图标
function cimg(shis,uid){
	var index = layer.load(2, {
		  shade: [0.1,'#fff'] 
	});
	var tds = $('.calendarwe .calendar-day');
	$.post('/work/attend/mywa',{
		uid:uid
	},function(data){
		$.each(tds, function(wew, domEl) {
			$(domEl).children("i").remove();
			var time=new Date($(domEl).attr('abbr')).getDay();
			//判断是否循环到今天
			var tesa=(new Date())>=(new Date($(domEl).attr('abbr')));
			
				var s=0;
				var yi=0;
				var qian=null;
				var tui=null;
				var que=0;
				var jiaq=0;
				var acw1=null;
				var acw2=null;
				var ji=null;
				$.each(data,function(n,wa){
					if (duibi(wa.time,$(domEl).attr('abbr'))) {
						if (wa.type=='签到') {
							//判断是否有申诉
							if (wa.remark!="无申诉"&&wa.status!=0) {
								acw1=wa.remark;
							}
							qian=wa;
							s+=1;
						}else if(wa.type=='签退'){
							//判断是否有申诉
							if (wa.remark!="无申诉"&&wa.status!=0) {
								acw2=wa.remark;
							}
							tui=wa;
							s+=1;
						}
						//判断异常
						if (wa.status!=0) {
							yi=1;
						}
						//判断是否缺勤
						if (wa.status==3||wa.status==4) {
							que=1;
						}
						//判断请假
						if (wa.status==5) {
							ji=wa;
							jiaq=1;
						}
					}
					if ((wew+1)==tds.length && (n+1)==data.length) {
						layer.close(index);
						if (shis==1) {
							laye5();
						}
					}
					
				})
				var i = $('<i>');
				if (qian!=null) {
					$(i).attr('type1',qian.type);
					$(i).attr('status1',qian.status);
					$(i).attr('id1',qian.id);
					$(i).attr('acw1',acw1);
				}
				if (tui!=null) {
					$(i).attr('type2',tui.type);
					$(i).attr('status2',tui.status);
					$(i).attr('id2',tui.id);
					$(i).attr('acw2',acw2);
				}
				if(jiaq==1){
					$(i).attr('remark',ji.remark);
					$(i).attr('id3',ji.id);
					$(i).attr('type',ji.type);
					i.attr('class', 'iconfont icon-jia1');
				}else if(que==1){
					i.attr('class', 'iconfont icon-icon2');
				}else if (yi==1 && s==2) {
					i.attr('class', 'iconfont icon-yiye');
				}else if (tesa && s==2) {
					i.attr('class', 'iconfont icon-qiandao');
				}else if(time==0||time==6) {
					i.attr('class', 'iconfont icon-xiukexuan');
				}else if(tesa){
					i.attr('class', 'iconfont icon-icon2');
				}
				$(domEl).append(i);
		})
	})
}

//处理说明
function laye3(id){
	layer.open({
		type: 1,
		skin: 'demo-class',
		skin: 'yourclass',
		offset: '100px',
		title :'申请',
		area: ['540px', '260px'], //宽高
		content: $(".com .result"),
		closeBtn: 1,
		btn:['提交', '取消'],
		btnAlign: 'c', 
		yes: function(index2, layero){
			if ($('.result textarea').val() == '') {
				layer.tips('<span style="color:#2e2d3c">请输入内容</span>',
						'.ap-r textarea', {
							tips : [ 1, '#F8F8F8' ]
						});
				return;
			}
			$.post('/work/attend/com',{
				id:id,
				state:2,
				result:$('.result textarea').val()
			},function(data){
				if (data) {
					layer.msg("处理成功！", {
						time : 1000
					});
					refresh(this);
				}
			})
			layer.close(index2);
		}
		,btn2: function(index2, layero){
			layer.close(index2);
		}
	});
	$('.layui-layer-ico').attr('href','javascript:void(0)');
}
//申诉
function laye(aid){
	layer.open({
		type: 1,
		skin: 'demo-class',
		skin: 'yourclass',
		offset: '100px',
		title :'申请',
		area: ['540px', '300px'], //宽高
		content: $(".attend .appeal"),
		closeBtn: 1,
		btn:['确定', '取消'],
		btnAlign: 'c', 
		yes: function(index2, layero){
			var content=$('.appeal .ap-r textarea').val();
			if (content == '') {
				layer.tips('<span style="color:#2e2d3c">请输入内容</span>',
						'.ap-r textarea', {
							tips : [ 1, '#F8F8F8' ]
						});
				return;
			}
			var reason=$('.appeal .ap-h span').text();
			//获取本地ip
			var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_=' + Math.random();
			$.getJSON(url, function (data) {
				var ip=data.Ip;//弹出本地ip
				$.post('/work/attend/complaint',{
					content:content,
					aid:aid,
					reason:reason,
					ip:ip
				},function(data){
					if (data) {
						layer.msg("提交申诉成功！", {
							time : 1000
						});
						layer.closeAll();
						refresh(this);
					}else {
						layer.msg("提交申诉失败！", {
							time : 1000
						});
					}
				})
		    });
			
			layer.close(index2);
		}
		,btn2: function(index, layero){
			$('.ap-r textarea').text('')
			layer.close(index);
		}
	});
	$('.layui-layer-ico').attr('href','javascript:void(0)');
}

//申诉处理
function laye2(id){
	layer.open({
		type: 1,
		skin: 'demo-class',
		skin: 'yourclass',
		offset: '100px',
		title :'申请',
		area: ['540px', '250px'], //宽高
		content: $(".com .chuli"),
		closeBtn: 1,
		btn:['同意', '退回'],
		btnAlign: 'c', 
		yes: function(index, layero){
			$.post('/work/attend/com',{
				id:id,
				state:1
			},function(data){
				if (data) {
					layer.msg("处理成功！", {
						time : 1000
					});
					$('.layui-layer-shade').remove();
					refresh(this);
				}
			})
			layer.close(index);
		}
		,btn2: function(index, layero){
			laye3(id);
			layer.close(index);
		}
	});
	$('.layui-layer-ico').attr('href','javascript:void(0)');
}


/**
 * 查看退回申诉
 */
function laye4(id){
	layer.open({
		type: 1,
		skin: 'demo-class',
		skin: 'yourclass',
		offset: '100px',
		title :'申请详情',
		area: ['540px', '340px'], //宽高
		content: $(".attend .appeal2"),
		closeBtn: 1,
		btn:['提交', '取消'],
		btnAlign: 'c', 
		yes: function(index2, layero){
			var content=$('.appeal2 .ap-r textarea').val();
			//console.log(content)
			if (content == '') {
				layer.tips('<span style="color:#2e2d3c">请输入内容</span>',
						'.appeal2 .ap-r textarea', {
							tips : [ 1, '#F8F8F8' ]
						});
				return;
			}
			$.post('/work/attend/complainttwe',{
				content:content,
				id:id
			},function(data){
				if (data) {
					layer.msg("重新提交申诉成功！", {
						time : 1000
					});
					refresh(this);
				}else {
					layer.msg("提交申诉失败！", {
						time : 1000
					});
				}
			})
			layer.close(index2);
		}
		,btn2: function(index2, layero){
			layer.close(index2);
		}
	});
	$('.layui-layer-ico').attr('href','javascript:void(0)');
}

/**
 * 基本方法
 * @param dp
 * @returns
 */
function sEnd(dp) {
	var end = dp.cal.getNewDateStr();
	if ((new Date($('.start-time').val())) > (new Date(end))) {
		layer.alert('结束时间不能小于开始时间');
		return true;
	} else {
		return false;
	}
}
function sStart(dp) {
	var start = dp.cal.getNewDateStr();
	if ((new Date($('.date .end-time').val())) < (new Date(start))) {
		layer.alert('开始时间不能大于结束时间');
		return true;
	} else {
		return false;
	}
}
/**
 * 补0
 */
function appendzero(obj) {
	if (obj < 10)
		return "0" + obj;
	else
		return obj;
}
//n=0获取本月开始时间
function getMonthStart(n) {
	var now = new Date();
	var year = now.getFullYear();
	// 因为月份是从0开始的,所以获取这个月的月份数要加1
	var month = now.getMonth() + 1 + n;
	s = year + "-" + appendzero(month) + "-" + "01";
	return s;

}
//获取当前时间
function getMonthEnd(n) {
	if (n==null) {
		var now = new Date();
	}else {
		var now = new Date(n);
	}
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var date = now.getDate();
	s = year + "-" + appendzero(month) + "-" + appendzero(date);
	return s;
}
//上几个月结束时间
function getMonthEnd2(n) {
	var now = new Date();
	var year = now.getFullYear();
	var month = now.getMonth() + 1 - n;
	if (month <= 0) {
		month = month + 12;
		year = year - 1;
	}
	var d = new Date(year, month, 0).getDate();
	s = year + "-" + appendzero(month) + "-" + appendzero(d);
	return s;
}
//阿拉伯数转中文0代表星期天
function zu(n ){
	if (n==1) {
		return'一';
	}else if (n==2) {
		return'二';
	}else if (n==3) {
		return'三';
	}else if (n==4) {
		return'四';
	}else if (n==5) {
		return'五';
	}else if (n==6) {
		return'六';
	}else if (n==0) {
		return'天';
	}
}
//查询某个时间段的考勤统计‘
function ton(now1,now2){
	refreshPanel(getUrlPrefix('/work/attend/check?start='+now1+"&end="+now2), "考勤报表")
}
function sdffe(){
	ton($('.date .start-time').val(),$('.date .end-time').val());
}
//比较两个日期是否是同一天
function duibi(a, b) {
    if (shieaw(a) != shieaw(b)) {
        return false;
    } else {
		return true;
	};
} 

//格式化时间 （yyyy-mm-dd）
function shieaw(da) {
	var s = new Date(da);
	var yyyy = s.getFullYear();
	var mm = appendzero(s.getMonth() + 1);
	var dd = appendzero(s.getDate());
	return yyyy + "-" + mm + "-" + dd ;
}
//初始化计时，和部分样式
function as(){
	//计时
	var si = setInterval(function() {
		var as=new Date();
		var h=as.getHours();
		var m=as.getMinutes();
		var s=as.getSeconds();
		$('.punch-clock .t').text(h+":"+m+":"+appendzero(s));
	},1000)
	//日期
	getMonthEnd();
	var s=new Date();
	$('.work-time span').text(getMonthEnd()+' 星期'+zu(s.getDay()));
	
}